use crate::NoSyncSend;
use core_graphics::geometry::CGRect;
use metal::*;
use objc::runtime::Class;
use objc::Message;
use objc_foundation::INSObject;
use objc_id::Id;

pub trait IMTKView: INSObject {
  fn with_frame_device(frame: CGRect, device: Id<MTLDevice>) -> Id<Self> {
    let cls: &Class = Self::class();
    unsafe {
      let obj: *mut Self = msg_send![cls, alloc];
      let obj: *mut Self = msg_send![obj, initWithFrame: frame device: device];
      Id::from_retained_ptr(obj)
    }
  }
}

pub struct MTKView {
  _marker: NoSyncSend,
}

unsafe impl Message for MTKView {}

impl INSObject for MTKView {
  fn class() -> &'static Class {
    Class::get("MTKView").unwrap()
  }

  fn new() -> Id<Self> {
    assert_main_thread!();
    let cls: &Class = Self::class();
    unsafe {
      let obj: *mut Self = msg_send![cls, alloc];
      let obj: *mut Self = msg_send![obj, init];
      Id::from_retained_ptr(obj)
    }
  }
}
impl IMTKView for MTKView {}
